	   /*------------------------------------------------------------------*/
/* --- STC MCU Limited ---------------------------------------------*/
/* --- STC12C56xx Series MCU A/D Conversion Demo -------------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
/* --- Web: www.STCMCU.com -----------------------------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* If you want to use the program or the program referenced in the  */
/* article, please specify in which data and procedures from STC    */
/*------------------------------------------------------------------*/

#include "ADC.h"


//void main()
//{
//    InitADC();                      //Init ADC sfr
//}



/*----------------------------
Get ADC result
----------------------------*/
unsigned char  GetADCResult(unsigned char ch)
{
    ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
    _nop_();                        //Must wait before inquiry
    _nop_();
    _nop_();
    _nop_();
    while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
    ADC_CONTR &= ~ADC_FLAG;         //Close ADC

    return ADC_DATA;                //Return ADC result
}



/*----------------------------
Initial ADC sfr
----------------------------*/
void InitADC()
{
    P1 = P1M0 = P1M1 = 0xff;        //Set all P1 as Open-Drain mode
    ADC_DATA = 0;                   //Clear previous result
    ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
    Delay(2);                       //ADC power-on and delay
}


/*----------------------------
Software delay function
----------------------------*/
void Delay(unsigned int n)
{
    unsigned int  x;

    while (n--)
    {
        x = 5000;
        while (x--);
    }
}